package cn.edu.fzu.homemaking.company.controller;

import cn.edu.fzu.homemaking.common.PageVO;
import cn.edu.fzu.homemaking.company.api.OrderApi;
import cn.edu.fzu.homemaking.company.api.SsoApi;
import cn.edu.fzu.homemaking.company.domain.entity.CompanyUser;
import cn.edu.fzu.homemaking.company.domain.entity.Order;
import cn.edu.fzu.homemaking.company.domain.entity.OrderCompanyUser;
import cn.edu.fzu.homemaking.company.domain.entity.RobotMsg;
import cn.edu.fzu.homemaking.company.domain.enums.OrderStatusEnum;
import cn.edu.fzu.homemaking.company.domain.enums.RobotMsgStatusEnum;
import cn.edu.fzu.homemaking.company.domain.enums.UserOrderStatusEnum;
import cn.edu.fzu.homemaking.company.domain.vo.OrderVO;
import cn.edu.fzu.homemaking.company.service.OrdersCompanyUserService;
import cn.edu.fzu.homemaking.company.util.UserInfoUtil;
import cn.edu.fzu.homemaking.util.ResultUtil;
import cn.edu.fzu.homemaking.web.Result;
import cn.edu.fzu.homemaking.web.RetCode;
import com.alibaba.spring.util.ObjectUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author wanjie
 * @version 1.0
 */
@RestController
@RequestMapping("/api/company/auth/orderCompanyUser")
@Api(tags = "公司端家政人员订单表相关接口-家政人员使用")
public class OrdersCompanyUserController {
    @Resource
    private OrdersCompanyUserService ordersCompanyUserService;
    @Resource
    private OrderApi orderApi;

    @GetMapping("/list")
    @ApiOperation(value = "订单家政人员表展示列表", httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "订单号"),
            @ApiImplicitParam(name = "userOrderStatus", value = "订单状态:0=待完成，1=进行中，2=已完成"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true)})
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result list(String orderId, UserOrderStatusEnum userOrderStatus, Integer pageNum, Integer pageSize,
                       HttpServletRequest request) {
        CompanyUser companyUser = UserInfoUtil.getUserInfo(request, false);
        //取出userId
        Long companyUserId = companyUser.getCompanyUserId();
        try (Page<List<OrderCompanyUser>> page = PageHelper.startPage(pageNum, pageSize)) {
            List<OrderCompanyUser> list = ordersCompanyUserService.list(orderId, companyUserId, userOrderStatus);
            List<OrderVO> result = new ArrayList<>(list.size());
            for (OrderCompanyUser orderCompanyUser : list) {
                OrderVO vo = new OrderVO();
                BeanUtils.copyProperties(
                        Objects.requireNonNull(
                                ResultUtil.parseValue(orderApi.single(orderCompanyUser.getOrderId()), Order.class)),
                        vo);
                vo.setUserOrderStatus(orderCompanyUser.getStatus());
                result.add(vo);
            }
            return new Result(new PageVO<>(page.getTotal(), result));
        }
    }


    @PostMapping("/update")
    @ApiOperation(value = "更新订单状态", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "orderId", required = true),
            @ApiImplicitParam(name = "userOrderStatus", value = "状态:0=待完成，1=进行中，2=已完成", required = true)})
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result updateOrderStatus(String orderId, UserOrderStatusEnum userOrderStatus, HttpServletRequest request) {
        boolean flag = true;
        CompanyUser companyUser = UserInfoUtil.getUserInfo(request, false);
        Long companyUserId = companyUser.getCompanyUserId();
        if (Objects.equals(userOrderStatus, UserOrderStatusEnum.DOING)) {
            orderApi.updateOrderStatus(orderId, OrderStatusEnum.GOING);
        } else if (Objects.equals(userOrderStatus, UserOrderStatusEnum.DONE)) {
            List<OrderCompanyUser> list = ordersCompanyUserService.list(orderId, null, null);
            for (OrderCompanyUser orderCompanyUser : list) {
                if (!Objects.equals(orderCompanyUser.getStatus(), UserOrderStatusEnum.DONE)
                        && !Objects.equals(orderCompanyUser.getCompanyUserId(), companyUserId)) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                orderApi.updateOrderStatus(orderId, OrderStatusEnum.PAYING);
            }
        }
        ordersCompanyUserService.updateOrderCompanyUser(orderId, userOrderStatus, companyUserId);
        return new Result(RetCode.SUCCESS, "更新订单家政人员表成功");
    }

}
